<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box1 {
        width: 100px;
        height: 100px;
        background-color: brown;
      }
      .box2 {
        width: 100px;
        height: 100px;
        background-color: green;
      }
      .box3 {
        width: 100px;
        height: 100px;
        background-color: yellow;
      }
    </style>
  </head>
  <body>
    <button>按钮一</button>
    <button>按钮二</button>
    <button>按钮三</button>
    <div class="box1">111</div>
    <div class="box2">222</div>
    <div class="box3">333</div>
    <script>
      var buttonEle = document.querySelectorAll("button");
      var divs = document.querySelectorAll("div");
      //   buttonEle[0].onclick = function () {
      //     divs.forEach(function (item) {
      //       item.style.display = "none";
      //     });

      //     divs[0].style.display = "block";
      //   };
      //   buttonEle[1].onclick = function () {
      //     divs.forEach(function (item) {
      //       item.style.display = "none";
      //     });

      //     divs[1].style.display = "block";
      //   };
      //   buttonEle[2].onclick = function () {
      //     divs.forEach(function (item) {
      //       item.style.display = "none";
      //     });

      //     divs[2].style.display = "block";
      //   };
      for (let i = 0; i < buttonEle.length; i++) {
        buttonEle[i].onclick = function () {
          for (j = 0; j < divs.length; j++) {
            divs[j].style.display = "none";
          }
          divs[i].style.display = "block";
        };
      }
    </script>
  </body>
</html>
